package com.wlc.base.array;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description Arrays工具类
 * @date 2021-06-24 14:18
 */
public class ArraysUtilDemo {

    public static int[] getArray() {
        int[] array = {12, 44, 522, 14, 1, 45, 67};
        return array;
    }
    public static int[] getArray2() {
        int[] array = {126, 44, 522, 14, 1, 45, 67};
        return array;
    }

    public void method01() {

        System.out.println("Arrays.toString(array) = " + Arrays.toString(getArray()));
        List<int[]> ints = Arrays.asList(getArray());
        for (int i = 0; i < ints.size(); i++) {
            for (int j = 0; j < ints.get(i).length; j++) {
                System.out.println("ints.get(i)[j] = " + ints.get(i)[j]);
            }
        }
    }

    public void method02() {
        int[] array = getArray();
        System.out.println(Arrays.toString(array));

        String json = "2,3,4,5,6,7,8";
        List<String> strings = Arrays.asList(json);

        System.out.println("strings.toString() = " + strings.toString());
        System.out.println("排序前 的数组为；" + Arrays.toString(getArray()));
        // 数组进行排序
        Arrays.sort(array);
        System.out.println("getArray() = " + Arrays.toString(array));
    }

    /**
     * Arrays.fill() 对数组进行赋值
     */
    public void arraysFill() {
        int[] array = getArray();
        // Arrays.fill()； 把2给array数组进行赋值
        Arrays.fill(array, 2);
        Arrays.fill(array,2,4,3);
        System.out.println("Arrays.toString(array) = " + Arrays.toString(array));
    }

    /**
     * Arrays.sort() 对数组进行排序
     */
    public void arraysSort(){
        int[] array = getArray();
        System.out.println("排序前的数组为：" + Arrays.toString(array));
        System.out.println("============");
        Arrays.sort(array);
        System.out.println("排序后 Arrays.toString(array) = " + Arrays.toString(array));
        System.out.println("======");
        // 降序排列， 反转
        System.out.println(Arrays.toString(reverse(array)));
    }

    /**
     * 对数组进行反转
     * @param array
     * @return
     */
    public int[] reverse(int[] array){
        int[] result = new int[array.length];
        for (int i = 0,j=array.length-1; i < array.length; i++,j--) {
            result[j]=array[i];
        }
        return result;
    }

    /**
     * Arrays.equals(int[] a,int[] b) 判断a ,b 2个数组是否相等
     */
    public void arraysEqual(){
        int[] array = getArray();
        int[] array2 = getArray2();
        System.out.println("Arrays.equals(array,array2) = " + Arrays.equals(array, array2));
    }

    public static void main(String[] args) {
        new ArraysUtilDemo().arraysFill();
    }
}
